import {
    msg,
    close,
    open
} from '../../common/utils.js';
import Vue from 'vue';
import {
    serverPath,
    json,
    currentPath
} from '../../common/config.js';
import TemplateService from '../services/templateService.js';
import _ from "lodash";

window.Module = function () {
    let templateService = new TemplateService();
    return new Vue({
        el: '#addPageController',
        data: {
            entity: {
                ApplyCrowds: [],
                Questions: [],
                Categorys: []
            }
        },
        methods: {
            add: async function () {
                if (!validator.checkAll('form')) {
                    layer.msg('请填写正确参数');
                    return;
                } 

                var self = this;
                this.entity.applyCrowds = [];
                this.entity.questions = [];
                this.entity.categorys = [];
                _.forEach(this.entity.ApplyCrowds, function (value) {
                    self.entity.applyCrowds.push(value.id);
                });
                _.forEach(this.entity.Categorys, function (value) {
                    self.entity.categorys.push(value.id);

                });
                _.forEach(this.entity.Questions, function (value) {
                    self.entity.questions.push(value.id);
                });

                let res = await templateService.createTemplates(this.entity);
                if (!res.type) {
                    for (var item in res) {
                        msg(res[item][0]);
                        break;
                    }
                }
                else {
                    var response = res.body;
                    if ('Success' == response.result) {
                        this.close();
                    }
                    else {
                        msg(response.message);
                    }
                }
            },
            close: function () {
                close();
            },
            addRole: async function () {
                let options = {
                    title: "选择角色",
                    content: `${currentPath}dialog.html#/TemplatePage/selectRolePage`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
                if (window.result) {
                    var index = _.findIndex(this.entity.ApplyCrowds, function (value) {
                        return value.id == window.result.id;
                    });
                    if (index < 0)
                        this.entity.ApplyCrowds.push(window.result);
                    delete window.result;
                    this.$forceUpdate();
                    console.log(this.entity);
                }
            },
            addQuestions: async function () {
                let options = {
                    title: "选择问题",
                    content: `${currentPath}dialog.html#/TemplatePage/selectQuestionPage`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
                if (window.result) {
                    var self = this;
                    _.forEach(window.result, function (value) {
                        var index = _.findIndex(self.entity.Questions, function (question) {
                            return question.id == value.id;
                        });
                        if (index < 0)
                            self.entity.Questions.push(value);
                    });
                    delete window.result;
                    this.$forceUpdate();
                }

            },
            addCategorys: async function () {
                let options = {
                    title: "选择分类",
                    content: `${currentPath}dialog.html#/TemplatePage/selectCategoryPage`,
                    maxmin: false,
                    area: ['80%', '70%']
                };
                await open(options);
                if (window.result) {
                    var index = _.findIndex(this.entity.Categorys, function (value) {
                        return value.id == window.result.id;
                    });
                    if (index < 0)
                        this.entity.Categorys.push(window.result);
                    delete window.result;
                    this.$forceUpdate();
                }

            },
            deleteCategory: function (index) {
                this.entity.Categorys.splice(index, 1);
            },
            deleteQuestion: function (index) {
                this.entity.Questions.splice(index, 1);
            },
            deleteRole: function (index) {
                this.entity.ApplyCrowds.splice(index, 1);
            }
        }
    });
}